package com.v1.video.adapter;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.v1.video.Constant;
import com.v1.video.R;
import com.v1.video.activity.PaikeVideoDetailActivity2;
import com.v1.video.activity.SecondVideoDetailActivity2;
import com.v1.video.domain.CommentEntity;
import com.v1.video.domain.LoginInfo;
import com.v1.video.domain.RequestData;
import com.v1.video.domain.SetHead;
import com.v1.video.httpApi.V1VideoHttpApi;
import com.v1.video.httpmanager.RequestManager;
import com.v1.video.httpmanager.RequestManager.OnResponseListener;
import com.v1.video.util.Logger;
import com.v1.video.util.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangfang on 2016/8/5.
 */
public class CommentAdapter extends BaseAdapter {
	public static final String VIDEO_FLAG = "0";
	public static final String SECOND_FLAG = "1";
	private Context mContext;
	private LayoutInflater mInflater;
	private List<CommentEntity.CommentInfo> mDataLst;
	private String mFlag = VIDEO_FLAG;
	private boolean isNoComment = true;

	public CommentAdapter(Context context, String flag) {
		mContext = context;
		mInflater = LayoutInflater.from(context);
		mFlag = flag;
		mDataLst = new ArrayList<CommentEntity.CommentInfo>();
	}

	public List<CommentEntity.CommentInfo> getDataLst() {
		return mDataLst;
	}
	
	public void setDataList(List<CommentEntity.CommentInfo> dataLst) {
		if (dataLst != null && dataLst.size() > 0) {
			mDataLst.addAll(dataLst);
			this.notifyDataSetChanged();
		}
	}

	public boolean isNoComment() {
		return isNoComment;
	}

	public void setNoComment(boolean isNoComment) {
		this.isNoComment = isNoComment;
	}

	@Override
	public int getCount() {
		if (isNoComment && mDataLst.size() == 0) {
			return 1;
		}
		return mDataLst.size();
	}

	@Override
	public Object getItem(int position) {
		return mDataLst.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (isNoComment && mDataLst.size() == 0) {
			View view = mInflater.inflate(R.layout.no_comment, null);
			return view;
		}
		Holder holder = null;
		if (convertView == null || convertView.getTag() == null) {
			convertView = mInflater.inflate(R.layout.activity_comment_item,
					null);
			holder = new Holder();
			holder.userImg = (ImageView) convertView
					.findViewById(R.id.iv_user_image);
			holder.name = (TextView) convertView.findViewById(R.id.tv_name);
			holder.time = (TextView) convertView.findViewById(R.id.tv_time);
			holder.content = (TextView) convertView
					.findViewById(R.id.tv_content);
//			holder.praise = (TextView) convertView.findViewById(R.id.tv_praise);
//			holder.tread = (TextView) convertView.findViewById(R.id.tv_tread);
			holder.reply = (TextView) convertView.findViewById(R.id.tv_reply);
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}
		final CommentEntity.CommentInfo info = mDataLst.get(position);
		new SetHead().setHead(info.getAvatar(), "", holder.userImg);
		if (!TextUtils.isEmpty(info.getNickname())) {
			holder.name.setText(info.getNickname());
		} else {
			holder.name.setText("");
		}
//		if (!TextUtils.isEmpty(info.getSupport_num())) {
//			holder.praise.setText(info.getSupport_num());
//		} else {
//			holder.praise.setText("0");
//		}
//		if (!TextUtils.isEmpty(info.getStamp_num())) {
//			holder.tread.setText(info.getStamp_num());
//		} else {
//			holder.tread.setText("0");
//		}
		String replyname = "";
		if (!TextUtils.isEmpty(info.getReply_nickname())) {
			replyname = "回复" + info.getReply_nickname();
		} else {
			if (!TextUtils.isEmpty(info.getReply_user_id())
					&& !"0".equals(info.getReply_user_id())) {
				replyname = "回复";
			}
		}
		if (!TextUtils.isEmpty(info.getTime())) {
			holder.time.setText(Utils.getTimeStampToFormatString(
					info.getTime(), "MM-dd HH:mm"));
		} else {
			holder.time.setText("");
		}
		String content = replyname;
		if (!TextUtils.isEmpty(info.getContent())) {
			if (!TextUtils.isEmpty(content)) {
				content = content + " " + info.getContent();
			} else {
				content = info.getContent();
			}
		}
		SpannableString sp = new SpannableString(content);
		sp.setSpan(
				new ForegroundColorSpan(mContext.getResources().getColor(
						R.color.color_005fff)), 0, replyname.length(),
				Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
		holder.content.setText(sp);
//		final TextView tvPraise = holder.praise;
//		holder.praise.setOnClickListener(new OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//				// TODO Auto-generated method stub
//				requestCommentPraise(info, tvPraise);
//			}
//		});
//		final TextView tvTread = holder.tread;
//		holder.tread.setOnClickListener(new OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//				// TODO Auto-generated method stub
//				requestCommentTread(info, tvTread);
//			}
//		});
		holder.reply.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (SECOND_FLAG.equals(mFlag)) {
					((SecondVideoDetailActivity2) mContext)
							.showSendCommentDialog(info.getId(),
									info.getNickname());
				} else {
					((PaikeVideoDetailActivity2) mContext)
							.showSendCommentDialog(info.getId(),
									info.getNickname());
				}
			}
		});
		return convertView;
	}

	private void requestCommentPraise(
			final CommentEntity.CommentInfo commentInfo, final TextView tvPraise) {
		String uid = "0";
		if (LoginInfo.getInstance().isLogin()) {
			uid = LoginInfo.getInstance().getUserId();
		}
		String url = String.format(V1VideoHttpApi.COMMENT_PRAISE(),
				Constant.COMMENT_VIDEO_TYPE, commentInfo.getObject_id(), uid,
				commentInfo.getId(), Constant.DEVICEID);
		Logger.i("requestCommentPraise", "点赞url=" + url);
		RequestManager.getInstance().getRequest(mContext, url,
				RequestData.class, new OnResponseListener() {

					@Override
					public void onSuccess(Object object, int code,
							String message, String keyId) {
						if (object != null) {
							RequestData entity = (RequestData) object;
							if (entity != null
									&& entity.getBody() != null
									&& !TextUtils.isEmpty(entity.getBody()
											.getResultCode())) {
								if ("1".equals(entity.getBody().getResultCode())) {
									int num = 0;
									if (!TextUtils.isEmpty(commentInfo
											.getSupport_num())) {
										num = Integer.parseInt(commentInfo
												.getSupport_num()) + 1;
									}
									commentInfo.setSupport_num(num + "");
									tvPraise.setText(num + "");
								} else {
									if (!TextUtils.isEmpty(entity.getBody()
											.getMessage())) {
										Toast.makeText(mContext,
												entity.getBody().getMessage(),
												Toast.LENGTH_SHORT).show();
									}
								}
							}
						}
					}

					@Override
					public void onFailure(int code, String message) {

					}
				});
	}

	private void requestCommentTread(
			final CommentEntity.CommentInfo commentInfo, final TextView tvTread) {
		String uid = "0";
		if (LoginInfo.getInstance().isLogin()) {
			uid = LoginInfo.getInstance().getUserId();
		}
		String url = String.format(V1VideoHttpApi.COMMENT_TREAD(),
				Constant.COMMENT_VIDEO_TYPE, commentInfo.getObject_id(), uid,
				commentInfo.getId(), Constant.DEVICEID);
		Logger.i("requestCommentPraise", "点赞url=" + url);
		RequestManager.getInstance().getRequest(mContext, url,
				RequestData.class, new OnResponseListener() {

					@Override
					public void onSuccess(Object object, int code,
							String message, String keyId) {
						if (object != null) {
							RequestData entity = (RequestData) object;
							if (entity != null
									&& entity.getBody() != null
									&& !TextUtils.isEmpty(entity.getBody()
											.getResultCode())) {
								if ("1".equals(entity.getBody().getResultCode())) {
									int num = 0;
									if (!TextUtils.isEmpty(commentInfo
											.getStamp_num())) {
										num = Integer.parseInt(commentInfo
												.getStamp_num()) + 1;
									}
									commentInfo.setStamp_num(num + "");
									tvTread.setText(num + "");
								} else {
									if (!TextUtils.isEmpty(entity.getBody()
											.getMessage())) {
										Toast.makeText(mContext,
												entity.getBody().getMessage(),
												Toast.LENGTH_SHORT).show();
									}
								}
							}
						}
					}

					@Override
					public void onFailure(int code, String message) {

					}
				});
	}

	private class Holder {
		public ImageView userImg;
		public TextView name;
		public TextView time;
		public TextView content;
//		public TextView praise;
//		public TextView tread;
		public TextView reply;
	}

}
